package com.jiyinit.web.core.business.dao;

import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.dao.mybatis.base.MybatisDaoContext;
import com.jiyinit.core.framework.dao.mybatis.paginator.domain.PageBounds;
import com.jiyinit.web.core.business.dto.OrgPageRequest;
import com.jiyinit.web.core.business.entity.TabCoreOrgEntity;
import com.jiyinit.web.core.common.util.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门管理dao
 *
 * @author zhaolaiwei
 * @version 1.0
 * @date 2017/4/18 09:11
 * @since 1.0
 */
@Repository("core.tabCoreOrgDao")
public class TabCoreOrgDao {
    /**
     * 日志对象
     */
    private static Logger logger = LoggerFactory.getLogger(TabCoreOrgDao.class);

    private String SQLID = "core.tabCoreOrgDao.";

    @Autowired
    @Qualifier("core.MybatisDaoContext")
    private MybatisDaoContext mybatisDaoContext;

    public int selectMaxInOrder(Integer parentId){
        return mybatisDaoContext.findOne(SQLID+"selectMaxInOrder",Integer.class,parentId);
    }

    /**
     * 根据id查询部门
     *
     * @param orgId
     * @return
     */
    public TabCoreOrgEntity selectById(Integer orgId) {
        return mybatisDaoContext.findOne(SQLID + "selectById", TabCoreOrgEntity.class, orgId);
    }

    /**
     * 顶级机构
     */
    public Integer findTopOrg(Integer orgId){
        return mybatisDaoContext.findOne(SQLID+"findTopOrg",Integer.class,orgId);
    }
    /**
     * 根据parentId查询部门
     *
     * @param parentId
     * @return
     */
    public List<TabCoreOrgEntity> selectByParentId(Integer parentId) {
    	Map<String, Object> map = new HashMap<String, Object>();
        map.put("parentId", parentId);
        return mybatisDaoContext.findAll(SQLID + "selectByParentId", TabCoreOrgEntity.class, map);
    }

    /**
     * 人员和机构一并展示
     * @param parentId
     * @return
     */
    public List<TabCoreOrgEntity> selectOrgAndPeoByPId(Integer parentId) {
    	Map<String, Object> map = new HashMap<String, Object>();
    	
        map.put("parentId", parentId);
        return mybatisDaoContext.findAll(SQLID + "selectOrgAndPeoByPId", TabCoreOrgEntity.class, map);
    }
    
    
    
    /**
     * 保存部门
     * @param tabCoreOrg
     * @return
     */
    public int save(TabCoreOrgEntity tabCoreOrg) {
        return mybatisDaoContext.save(SQLID + "save", tabCoreOrg);
    }

    /**
     * 修改部门
     * @param
     * @return
     */
    public int update(TabCoreOrgEntity tabCoreOrg) {
        return mybatisDaoContext.update(SQLID + "update", tabCoreOrg);
    }

    /**
     * 通过parentId分页查询
     * @param orgPageRequest
     * @return
     */
    public PagingResult<TabCoreOrgEntity> selectPageByParentId(OrgPageRequest orgPageRequest){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(orgPageRequest.getLimit());
        pageBounds.setPage(orgPageRequest.getPage());
        return mybatisDaoContext.findForPage(SQLID + "selectByParentId", TabCoreOrgEntity.class, orgPageRequest.toMap(), pageBounds);
    }

    /**
     * 通过parentIds查询部门信息
     * @param parentIds
     * @return
     */
    public List<TabCoreOrgEntity> selectByParentIds(Integer[] parentIds) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("parentIds", parentIds);
        return mybatisDaoContext.findAll(SQLID + "selectByParentIds", TabCoreOrgEntity.class, map);
    }

    /**
     * 逻辑删除部门信息
     * @param orgIds
     * @return
     */
    public int deleteLogicByIds(Integer[] orgIds){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orgIds", orgIds);
        map.put("lastModifyId", UserUtil.getCurrentUser().getUserId());
        map.put("lastModifyTime",new Date());
        return mybatisDaoContext.update(SQLID + "deleteLogicByIds", map);
    }

    public Integer getTopParentOrgId(Integer orgId){
        return mybatisDaoContext.findOne(SQLID+"getTopParentOrgId",Integer.class,orgId);
    }

    public List<TabCoreOrgEntity> selectAll(){
        return mybatisDaoContext.findAll(SQLID + "selectAll");
    }

}
